package IO流.Reader;

import org.junit.jupiter.api.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
@SuppressWarnings("all")
public class FileReader01 {
    @Test
    public void fileRead001(){
        String filePath = "src/main/java/IO流/Reader/hello.txt";
        FileReader fileReader = null;
        int data = 0;

        try {
            fileReader = new FileReader(filePath);
            //单个字符读取，每次读取一个字符
            while ((data = fileReader.read()) != -1){
                System.out.print((char)data);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void fileRead002(){
        String filePath = "src/main/java/IO流/Reader/hello.txt";
        FileReader fileReader = null;
        int dataLne = 0;
        char[] buff = new char[8];
        try {
            fileReader = new FileReader(filePath);
            //通过将字符读入到char[]数组
            //dataLne代表读取的字符数组的实际长度
            while ((dataLne = fileReader.read(buff)) != -1){
                System.out.print(new String(buff,0,dataLne));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
